import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';

@Pipe({
  name: 'keyLight'
})
export class KeyLightPipe implements PipeTransform {
  constructor(private sanitizer: DomSanitizer) { }

  transform(text: string, words: any): unknown {
    const s = [];
    let start = 0;
    // eslint-disable-next-line guard-for-in
    for (const k in words) {
      const l = parseInt(k, 10);
      const r = words[k];
      s.push(text.substring(start, l));
      s.push('<span style="color:red">');
      s.push(text.substring(l, r));
      s.push('</span>');
      start = r;
    }

    s.push(text.substring(start));

    return this.sanitizer.bypassSecurityTrustHtml(s.join(''));
  }

}
